package com.bfly.mall.wechat.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bfly.common.ResponseUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.security.ActionModel;
import com.bfly.mall.wechat.service.IWeChatMpMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 微信公众号自定义菜单 Controller
 *
 * @author 长沙商脉科技有限公司
 * @date 2021/1/4 14:36
 */
@RestController
@RequestMapping(value = {"/manage/wechat/mp/menu"})
public class WeChatMpMenuController extends BaseController {

    @Autowired
    private IWeChatMpMenuService weChatMpMenuService;

    /**
     * 微信公众号菜单列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2021/1/4 14:38
     */
    @GetMapping(value = "/list")
    public void mpMenuList(HttpServletResponse response, @RequestParam("platformId") int platformId) {
        JSONArray array = weChatMpMenuService.getWeChatMenu(platformId);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(array));
    }

    /**
     * 编辑微信公众号菜单
     *
     * @author 长沙商脉科技有限公司
     * @date 2021/1/4 14:59
     */
    @PostMapping(value = "/merge")
    @ActionModel("编辑微信公众号菜单")
    public void mergeWeChatMpMenu(HttpServletResponse response, @RequestBody JSONObject json) {
        int platformId = json.getInteger("platformId");
        JSONArray jsons = json.getJSONArray("menus");
        weChatMpMenuService.merge(platformId, jsons);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 删除微信公众号菜单
     *
     * @author 长沙商脉科技有限公司
     * @date 2021/1/4 15:26
     */
    @PostMapping(value = "/delete")
    @ActionModel(value = "删除微信公众号菜单")
    public void removeWeChatMpMenu(HttpServletResponse response, @RequestBody Integer... Ids) {
        weChatMpMenuService.remove(Ids);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 发布菜单
     *
     * @author 长沙商脉科技有限公司
     * @date 2021/1/7 15:46
     */
    @GetMapping(value = "/push")
    @ActionModel(value = "发布微信公众号菜单")
    public void pushWeChatMpMenu(HttpServletResponse response, @RequestParam("platformId") int platformId) {
        weChatMpMenuService.pushMenuToWeChat(platformId);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }
}